/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.futuremind.recyclerviewfastscroll;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * Utils
 *
 * @since 2021-03-24
 */
public class ListContainerScrolledListener implements Component.ScrolledListener {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x2021, "ListContainerScrolledListener");
    private static final int NUM500 = 500;
    private static final int NUM600 = 600;
    private static final int NUMFF = 0xff;
    private static final int NUM40 = 0x40;
    private static final int NUM81 = 0x81;
    List<ScrolledListener> listeners = new ArrayList<>();
    private final FastScroller scrolled;


    public ListContainerScrolledListener(FastScroller scrolled) {
        this.scrolled = scrolled;
    }

    /**
     * listeneraddScrolledListener
     *
     * @param listener q
     */
    public void addScrolledListener(ScrolledListener listener) {
        listeners.add(listener);
    }

    @Override
    public void onContentScrolled(Component component, int i0, int i1, int i2, int i3) {
        ListContainer listContainer = (ListContainer) component;
        scrolled.dispatchOnContentScrolled(component, i0, i1, i2, i3);
        if (scrolled.shouldUpdateHandlePosition()) {
            updateHandlePosition(listContainer);
        }
    }

    void updateHandlePosition(ListContainer lc) {
        float relativePos;
        if (lc.getOrientation() == Component.VERTICAL) {
            int offset = lc.getScrollValue(Component.AXIS_Y); // 已经向下滚动的距离，为0时表示已处于顶部
            int extent = lc.getHeight(); // 显示范围
            int range = scrolled.getTotalScrollH(); // 垂直滚动条表示的全部水平滚动范围
            relativePos = offset / (float) (range - extent);
            setView(Utils.getDependentLayout(), Utils.getTextview(), offset);
        } else {
            int offset = lc.getScrollValue(Component.AXIS_X); // 在X轴滚动的距离，为0时表示已处于顶部
            int extent = lc.getWidth(); // 显示范围
            int range = scrolled.getTotalScrollW(); // 水平滚动条表示的全部水平滚动范围
            relativePos = offset / (float) (range - extent);
        }
        scrolled.setScrolledPosition(relativePos);
        notifyListeners(relativePos);
    }

    /**
     * notifyListenersif
     *
     * @param relativePos q
     */
    public void notifyListeners(float relativePos) {
        for (ScrolledListener listener : listeners) {
            listener.onScroll(relativePos);
        }
    }

    /**
     * UtiScrolledListenerls
     *
     * @since 2021-03-24
     */
    public interface ScrolledListener {
        /**
         * onScroll
         *
         * @param relativePos q
         */
        void onScroll(float relativePos);
    }

    /**
     * setView
     *
     * @param deid     q
     * @param testview q
     * @param offset   q
     */
    public static void setView(DependentLayout deid, Text testview, int offset) {
        if (offset == 0) {
            deid.setVisibility(Component.VISIBLE);
            deid.setHeight(NUM500);
            testview.setVisibility(Component.HIDE);
        } else if (offset > NUM600) {
            deid.setVisibility(Component.HIDE);
            testview.setVisibility(Component.VISIBLE);
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(NUMFF, NUM40, NUM81));
            testview.setBackground(element);
        } else {
            Utils.getDependentLayout().setHeight(offset);
        }
    }
}
